package com.cn.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.cn.dao.CommentsDao;
import com.cn.domain.Comment;
import com.cn.utils.DBUtils;

public class CommentsDaoImpl implements CommentsDao {
	//注册留言
	@Override
	public int addComents(Comment comment) {
		int i=0;
		try {
			Object[] params= {comment.getHc_content(),comment.getHc_nick_name()};
			i=DBUtils.getQuryRunner().update("insert into HWUA_COMMENT(HC_ID,HC_CONTENT,HC_CREATE_TIME,HC_NICK_NAME) values(SEQ_COMMENT.nextval,?,sysdate,?)",params);
			//System.out.println("i="+i);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}
	
	//按照分页查询所有的留言
	@Override
	public List<Comment> commentList(int pageNow,int pageSize){
		List<Comment> list=null;
		try {
			int num=(pageNow-1)*pageSize;
			int end=pageNow*pageSize; 
			String sql="select * from (select t1.*,rownum rn from (select * from hwua_comment order by HC_CREATE_TIME desc) t1 where rownum<="+end+") where rn>"+num+"";
			list=DBUtils.getQuryRunner().query(sql,new BeanListHandler<Comment>(Comment.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	//获取所有的留言用于得到总数量到时候进行分页可以用
	@Override
	public List<Comment> getAllCount() {
		List<Comment> list=null;
		try {
			list=DBUtils.getQuryRunner().query("select * from hwua_comment",new BeanListHandler<Comment>(Comment.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
}
